package com.simulation.web.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.simulation.common.utils.SecurityUtils;
import com.simulation.web.domain.TechTeams;
import com.simulation.web.dto.TaskTeamMemberDTO;
import com.simulation.web.mapper.TaskMapper;
import com.simulation.web.mapper.TaskSubmissionsMapper;
import com.simulation.web.mapper.TeamMapper;
import com.simulation.web.mapper.TeamMemberMapper;
import com.simulation.web.service.TeamService;
import com.simulation.web.vo.TechTaskSubmissionsVO;
import com.simulation.web.vo.TechTaskVO;
import com.simulation.web.vo.TechTeamMembersVO;
import com.simulation.web.vo.TechTeamVO;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

@Service
@AllArgsConstructor
public class TeamServiceImpl implements TeamService {

    private final TeamMapper teamMapper;

    private final TeamMemberMapper teamMemberMapper;

    private final TaskMapper taskMapper;

    private final TaskSubmissionsMapper taskSubmissionsMapper;

    @Override
    public int insert(TechTeams team) {
        team.setTeamCode(UUID.randomUUID().toString());
        team.setCreateUserId(SecurityUtils.getUserId());
        team.setCreateTime(LocalDateTime.now());
        return teamMapper.insert(team);
    }

    @Override
    public int delete(Long id) {
        // 团队成员
        teamMemberMapper.deleteMembersByTeamId(id);
        List<TechTaskVO> taskVOList = taskMapper.getTasksByTeamId(id);
        if (CollectionUtil.isNotEmpty(taskVOList)) {
            List<Long> longList = taskVOList.stream().map(TechTaskVO::getId).collect(Collectors.toList());
            //团队成员任务
            taskSubmissionsMapper.deleteByTaskIds(longList);
            //团队任务
            taskMapper.deleteTaskByTeamId(id);
        }
        return teamMapper.delete(id);
    }

    @Override
    public List<TechTeamVO> getTeamsByLeaderId(Long createUserId) {
        return teamMapper.getTeamsByLeaderId(createUserId);
    }

    @Override
    public TechTeamVO getTeamById(Long id) {
        return teamMapper.getTeamById(id);
    }

    @Override
    public int update(TechTeams team) {
        return teamMapper.update(team);
    }

    @Override
    public TechTeamVO getTeamByTeamCode(String teamCode) {
        return teamMapper.getTeamByTeamCode(teamCode);
    }

    @Override
    public List<TechTeamVO> getTeamByTeamUserId(Long userId) {
        return teamMapper.getTeamByTeamUserId(userId);
    }

    @Override
    public PageInfo<TechTeamMembersVO> getTeamPageByuserId(TaskTeamMemberDTO taskTeamMemberDTO) {
        PageHelper.startPage(taskTeamMemberDTO.getPageNum(), taskTeamMemberDTO.getPageSize());
        List<TechTeamMembersVO>  teamVO = teamMemberMapper.getTeamUsersById(taskTeamMemberDTO.getTeamId());
        return new PageInfo<>(teamVO);
    }
}